package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import ldh.im.fxbase.richtext.*;
import ldh.im.fxbase.richtext.item.EmojiTextItem;
import ldh.im.fxbase.richtext.item.ImageTextItem;
import org.fxmisc.richtext.model.ReadOnlyStyledDocument;
import org.reactfx.util.Either;

public class ImTextAreaTest extends Application {

    private ImTextArea imTextArea = new ImTextArea();

    @Override
    public void start(Stage primaryStage) throws Exception {
//        imTextArea.setParagraphGraphicFactory( new BulletFactory(imTextArea));

        VBox vBox = new VBox();
        vBox.getChildren().addAll(createToolBar(), imTextArea);
        VBox.setVgrow(imTextArea, Priority.ALWAYS);

        Scene scene = new Scene(vBox, 800, 600);

        scene.getStylesheets().add(this.getClass().getResource("/demo/texta.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        imTextArea.requestFocus();
    }

    private ToolBar createToolBar() {
        ToolBar toolBar = new ToolBar();
        Button insertEmoji = new Button("insert emoji");
        insertEmoji.setOnAction(e->{
            ReadOnlyStyledDocument<Void, Either<String, TextItem>, String> ros =
                    ReadOnlyStyledDocument.fromSegment(Either.right(new EmojiTextItem("1f1e7-1f1e6")), null, "emoji", imTextArea.getSegOps());
            imTextArea.replaceSelection(ros);
        });

        Button insertImage = new Button("insert image");
        insertImage.setOnAction(e->{
            ReadOnlyStyledDocument<Void, Either<String, TextItem>, String> ros =
                    ReadOnlyStyledDocument.fromSegment(Either.right(new ImageTextItem("F:\\tmp\\capture\\1633791632244.png")), null, "image", imTextArea.getSegOps());
            imTextArea.replaceSelection(ros);
        });

        Button copy = new Button("copy");
        copy.setOnAction(e->{
            imTextArea.copy();
        });

        Button paste = new Button("paste");
        paste.setOnAction(e->{
            imTextArea.paste();
        });


        Button getData = new Button("Get Data");
        getData.setOnAction(e->{
            System.out.println("data:" + imTextArea.getData());
        });

        toolBar.getItems().addAll(insertEmoji, insertImage, copy, paste, getData);
        return toolBar;
    }

}
